from qiyeweixin.page_objects.page_base import PageBase
from selenium.webdriver.common.by import By
from qiyeweixin.utils_tool.logging_info import logger
# from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from qiyeweixin.utils_tool.web_utils import click_exception_user_defined

'''通讯录页面'''
class PageAddressBook(PageBase):

    __BTN_PLUS_SIGN = (By.CSS_SELECTOR, ".member_colLeft_top_addBtn")
    __BTN_ADD_DEPARTMENT = (By.CSS_SELECTOR, ".js_create_party")
    __BTN_DELETE_MEMBER = (By.XPATH, "//*[@class='ww_operationBar']//*[text()='删除']")
    __BTN_CORFIRM = (By.XPATH, "//*[text()='确认']")
    __BTN_DELETE_DEPARTMENT = (By.XPATH, "//body/ul//*[text()='删除']")
    __BTN_CORFIRM_1 = (By.XPATH, "//*[text()='确定']")
    __BTN_ADD_MEMBER = (By.XPATH, "//*[text()='添加成员']")
    __BTN_HOME_MENU = (By.XPATH, "//*[text()='首页']")

    __MSG_ADD_MEMBER_RESULT = (By.XPATH, "//*[text()='保存成功']")
    __MSG_DELETE_MEMBER_RESULT = (By.XPATH, "//*[text()='删除成功']")
    __MSG_ADD_DEPARTMENT_RESULT = (By.XPATH, "//*[text()='新建部门成功']")
    __MSG_DELETE_DEPARTMENT_RESULT = (By.XPATH, "//*[text()='删除部门成功']")

    #通讯录页面：断言【添加成员】操作的测试结果
    def get_add_memeber_result(self):
        logger.info("3、通讯录页面：断言【添加成员】操作的测试结果")
        element = self.wait_expect_element_visibility(self.__MSG_ADD_MEMBER_RESULT)
        add_result = element.text
        return add_result

    #清楚脏数据，通讯录页面：选中指定的成员后删除
    def delete_member(self):
        logger.info("4、通讯录页面：选中指定的成员后删除")
        __BTN_SELECT_USER = (By.XPATH, f"//*[text()='{self.username}']/../..//*[@class='ww_checkbox']")

        self.do_click(__BTN_SELECT_USER)
        self.do_click(self.__BTN_DELETE_MEMBER)
        self.do_click(self.__BTN_CORFIRM)

        return PageAddressBook(self.driver)

    #通讯录页面：断言【删除成员】操作的测试结果
    def get_delete_member_result(self):
        logger.info("5、通讯录页面：断言【删除成员】操作的测试结果")
        element = self.wait_expect_element_visibility(self.__MSG_DELETE_MEMBER_RESULT)
        del_result = element.text
        return del_result

    #通讯录页面：点击【添加成员】按钮
    def click_add_member(self):
        logger.info("2、通讯录页面：点击【添加成员】按钮")
        WebDriverWait(self.driver, 10).until(
            click_exception_user_defined(self.__BTN_ADD_MEMBER))

        from qiyeweixin.page_objects.page_create_member import PageCreateMember
        return PageCreateMember(self.driver)

    # 通讯录页面：点击左侧“+”按钮,然后点击【添加部门】按钮
    def click_add_depart(self):
        logger.info("2、通讯录页面：点击左侧“+”按钮,然后点击【添加部门】按钮")
        self.do_click(self.__BTN_PLUS_SIGN)
        self.do_click(self.__BTN_ADD_DEPARTMENT)

        from qiyeweixin.page_objects.page_create_department import PageCreateDepartment
        return PageCreateDepartment(self.driver)

    #通讯录页面：获取【新增部门】操作的测试结果
    def get_add_department_result(self):
        logger.info("4、通讯录页面：获取【新增部门】操作的测试结果")
        ele = self.wait_expect_element_visibility(self.__MSG_ADD_DEPARTMENT_RESULT)
        add_result = ele.text
        return add_result

    #清楚脏数据，通讯录页面：选中制定的部门进行删除
    def delete_department(self):
        logger.info("5、通讯录页面：选中制定的部门进行删除")
        __BTN_SELECT_DEPARTMENT = (By.XPATH, f"//*[text()='{self.district}区']/span")

        self.do_click(__BTN_SELECT_DEPARTMENT)
        self.do_click(self.__BTN_DELETE_DEPARTMENT)
        self.do_click(self.__BTN_CORFIRM_1)

        return PageAddressBook(self.driver)

    #通讯录页面：断言【删除部门】操作的测试结果
    def get_delete_department_result(self):
        logger.info("6、通讯录页面：断言【删除部门】操作的测试结果")
        ele = self.wait_expect_element_visibility(self.__MSG_DELETE_DEPARTMENT_RESULT)
        del_result = ele.text
        return del_result

    # 首页：点击【首页】菜单
    def click_home_menu(self):
        logger.info("1、首页：点击【首页】菜单")
        WebDriverWait(self.driver, 10).until(
            click_exception_user_defined(self.__BTN_HOME_MENU))

        from qiyeweixin.page_objects.page_home import PageHome
        return PageHome(self.driver)



